package com.murfrosoft.game;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Build;
import android.view.WindowManager;


import com.murfrosoft.R;
import com.murfrosoft.R.drawable;
import com.murfrosoft.entity.AirHockey.CompPaddle;
import com.murfrosoft.entity.AirHockey.Paddle;
import com.murfrosoft.entity.AirHockey.Puck;
import com.murfrosoft.entity.AirHockey.UserPaddle;
import com.murfrosoft.gameengine.SingleTouchGameEngine;
import com.murfrosoft.service.CollisionDetectorService;
import com.murfrosoft.service.CollisionDetectorServiceImpl;

public class AirHockeyGame2 extends SingleTouchGameEngine{
	/* Declare screen size variables ??? (can we auto-detect this?) */
	int HEIGHT = 200;
	int WIDTH = 300;
	
	Bitmap gameBoard;
	
	int score1 = 0;
	int score2 = 0;
	
	CollisionDetectorService service;
	
	Puck puck;
	CompPaddle compPaddle;
	UserPaddle userPaddle;
	
	
	public AirHockeyGame2(Context context,int width,int height) {
		super(context);

		WIDTH=width;
		HEIGHT=height;
		
		service=new CollisionDetectorServiceImpl();
		

		BitmapFactory.Options options= new BitmapFactory.Options();
		options.inPreferredConfig=Bitmap.Config.ARGB_8888;

		
		Bitmap bitmap=BitmapFactory.decodeResource(getResources(), drawable.rink,options);
		gameBoard = Bitmap.createScaledBitmap(bitmap, WIDTH, HEIGHT, false);
		
		
		
		
		puck = new Puck(context);
		puck.setRadius((int)WIDTH/15);
		puck.setFriction(.99f);
		puck.setPositionX(WIDTH/2);
		puck.setPositionY(HEIGHT/2);
		puck.setVelocityY(0);
		puck.setVelocityX(0);
		bitmap = BitmapFactory.decodeResource(getResources(),drawable.puck,options);
		Bitmap puckBitmap = Bitmap.createScaledBitmap(bitmap, (int)puck.getRadius()*2, (int)puck.getRadius()*2, false);
		puck.setBitmap(puckBitmap);
		
		compPaddle = new CompPaddle();
		compPaddle.setRadius(WIDTH/10);
		compPaddle.setPositionX(200);
		compPaddle.setPositionY(100);
		compPaddle.setVelocityX(2);
		compPaddle.setVelocityY(5);
		bitmap=BitmapFactory.decodeResource(getResources(),drawable.paddle_b, options);
		Bitmap paddleBlue= Bitmap.createScaledBitmap(bitmap, (int)compPaddle.getRadius()*2, (int)compPaddle.getRadius()*2, false);
		compPaddle.setBitmap(paddleBlue);
		
		userPaddle = new UserPaddle();
		userPaddle.setRadius(WIDTH/10);
		userPaddle.setOldPositionX(100);
		userPaddle.setOldPositionY(600);
		userPaddle.setPositionX(100);
		userPaddle.setOldPositionY(600);
		userPaddle.setVelocityX(0);
		userPaddle.setVelocityY(0);
		
	}



	@Override
	public void updatePhysics() {
		/* UPDATE PUCK POSITION */
		int s;
		
        compPaddle.move(WIDTH, HEIGHT, puck.getPositionX(), puck.getPositionY());
        userPaddle.move(WIDTH, HEIGHT, touch.x, touch.y);
    	puck.move(WIDTH,HEIGHT);
	

		puck.do_collision(compPaddle);
		puck.do_collision2(userPaddle);		// trying some new collision math.
		
		s = puck.check_score(WIDTH, HEIGHT);
		if( s == -1 ){
			score2 += 1;
			puck.reset(WIDTH, HEIGHT, 2);
		}
		else if( s == 1 ){
			score1 += 1;
			puck.reset(WIDTH, HEIGHT, 1);
		}
		
		/* CHECK FOR COLLISIONS WITH OUR PADDLE */
        //service.checkForCollision(puck, userPaddle);
		
		/* Check for collision with computer paddle */

		//service.checkForCollision(puck, compPaddle);

		
	}
	

	@Override
	public void draw(Canvas canvas) {
    
		/* NOTES:  James, I don't know where to place globals, or new classes, 
		 *   		in this code.  I'm doing a practice run using non-objects in the
		 *          draw method.  This is messy and inefficient, but should be playable
		 *          as a mock-up.  I'll need your help to do this properly!
		 */
		
		/* DRAW SOME SORT OF BOARD */
	    canvas.drawColor(Color.rgb(200,200,200));
	    Paint line_color = new Paint();
	    line_color.setColor(Color.rgb(0,180,0));
	    canvas.drawRect(0, HEIGHT/2+4, WIDTH,  HEIGHT/2-4, line_color);
	    // Draw goals
	    canvas.drawRect(WIDTH/2 - 130, 0, WIDTH/2 + 130, 40, line_color);
	    canvas.drawRect(WIDTH/2 - 130, HEIGHT-40, WIDTH/2 + 130, HEIGHT, line_color);
	    
	    
	    
	    /* Debug Text */
		Paint paint = new Paint();
		canvas.drawBitmap(gameBoard,0,0,paint);
		paint.setColor(Color.BLACK);
		paint.setTextSize(20);
		canvas.drawText("X:" + touch.x +" Y:" + touch.y, 20, 20, paint);
		canvas.drawText("Xv:" + userPaddle.getVelocityX() + " Yv:" + userPaddle.getVelocityY(), 20,40, paint);
		canvas.drawText(puck.getVelocityX() + "  " + puck.getVelocityY(), 20, 60, paint);
		paint.setTextSize(50);
		paint.setColor(Color.BLUE);
		canvas.drawText("" + score1, 0.85f*WIDTH, 0.4f*HEIGHT, paint);
		paint.setColor(Color.RED);
		canvas.drawText("" + score2, 0.85f*WIDTH, 0.6f*HEIGHT, paint);
	
		/* DRAW PUCK */

		
		
		puck.draw(canvas);
		compPaddle.draw(canvas);
		userPaddle.draw(canvas);

		
	
		
	}

}
